package com.sylingd.blog.lib

import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.provider.Settings
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import com.sylingd.blog.BaseApplication
import com.sylingd.blog.R

object Permission {
	fun check(permission: String): Boolean {
		return ContextCompat.checkSelfPermission(BaseApplication.instance, permission) == PackageManager.PERMISSION_GRANTED
	}

	fun isAutoRefuse(mActivity: Activity, permission: String): Boolean {
		return ActivityCompat.shouldShowRequestPermissionRationale(mActivity, permission)
	}

	fun request(mActivity: Activity, permission: Array<String>, code: Int) {
		ActivityCompat.requestPermissions(mActivity, permission, code)
	}
	fun request(mActivity: Activity, permission: String, code: Int) = request(mActivity, arrayOf(permission), code)

	fun showDialog(mActivity: Activity) {
		val dialog = AlertDialog.Builder(mActivity)
		//dialog.setTitle(mActivity.getString(R.string.authorization_prompt))
		dialog.setMessage(mActivity.getString(R.string.refuse_permission))
		dialog.setNegativeButton(mActivity.getString(R.string.cancel), { _, _ -> })
		dialog.setPositiveButton(mActivity.getString(R.string.grant_now), { _, _ ->
			val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
			val name = mActivity.packageName
			intent.data = Uri.parse("package:$name")
			mActivity.startActivity(intent)
		})
		dialog.show()
	}

}